<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 15:18
 */
ini_set('date.timezone','Asia/Shanghai');
header("Content-type: text/html; charset=utf-8");
$http = new swoole_http_server("0.0.0.0", 8855);
/**
 * 底层收到Http请求会先判断document_root路径下是否存在此文件，如果存在会直接发送文件内容给客户端，不再触发onRequest回调
 */
$http->set([
    'worker_num' => 5,
    'enable_static_handler' => true,
    'document_root' => "/data/wwwroot/swoole/data",
]);
$http->on('request', function($request, $response){
    $content = [
        'date:' => date("Y-m-d H:i:s"),
        'get:' => isset($request->get) ? $request->get : '',
        "post:" => isset($request->post) ? $request->post : '',
        'header:'=> isset($request->header) ? $request->header : '',
    ];
    swoole_async_writefile(__DIR__ . '/access.log', json_encode($content). PHP_EOL, function($filename){
        echo "filename:{$filename} , write ok!" . PHP_EOL;
        //echo "write ok." . PHP_EOL;
    }, FILE_APPEND);
    //$response->cookie('test', 'singwa', time() + 1800);
    $response->end("data" . json_encode($request->get));
});

$http->start();